package cn.dengta.webapp.user.entity;

import java.util.Date;
import javax.annotation.Nullable;

import cn.dengta.common.model.ValueProxy;
import cn.dengta.context.model.LabelI18nProxy;
import cn.dengta.webapp.base.entity.BizEntity;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.StringUtil;
import me.codeplayer.util.X;

import static cn.dengta.context.model.UserEnumI18nKey.*;

/**
 * 动态操作记录表（点赞、打赏、评论点赞）
 *
 * @author lgy
 * @since 2022-03-02
 */
@Setter
@Getter
@Accessors(chain = true)
public class MomentOperateLog extends BizEntity {

	/** 用户id */
	private Long userId;
	/** 操作类型 */
	private MomentOperate type;
	/** 动态ID */
	private Long momentId;
	/** 动态的评论ID */
	private Long commentId;
	/** 他人用户id */
	private Long toUserId;
	/** 数量 */
	private Integer count;
	/** 备注信息 */
	private String remark;
	/** 业务处理 0=未处理；1=已处理 */
	private Integer handleStatus;

	public void setId(Long id) {
		this.id = id;
	}

	public MomentOperateLog init(Long userId, Long toUserId, MomentOperate type, Long momentId, @Nullable Long commentId, Integer count, String remark, Date now) {
		this.userId = userId;
		this.toUserId = toUserId;
		this.type = type;
		this.momentId = momentId;
		this.commentId = commentId;
		this.count = X.expectNotNull(count, 1);
		this.remark = StringUtil.toString(remark);
		super.initTime(now);
		super.initActiveStatus();
		return this;
	}

	@Getter
	public enum MomentOperate implements LabelI18nProxy<MomentOperate, Integer> {
		/** 动态点赞 */
		DIG(0, MOMENT_OPERATE_DIG, true, 1, Moment.class),
		/** 取消动态点赞 */
		CANCEL_DIG(1, MOMENT_OPERATE_CANCEL_DIG, false, 1, Moment.class),
		/** 动态打赏 */
		DONATE(2, MOMENT_OPERATE_DONATE, true, Moment.class),
		/** 评论点赞 */
		COMMENT_DIG(3, MOMENT_OPERATE_COMMENT_DIG, true, 1),
		/** 取消评论点赞 */
		CANCEL_COMMENT_DIG(4, MOMENT_OPERATE_CANCEL_COMMENT_DIG, false, 1),
		//
		/** 评论 */
		COMMENT(5, MOMENT_OPERATE_COMMENT, true, 0),
		/** 评论删除 */
		COMMENT_DEL(6, MOMENT_OPERATE_COMMENT_DEL, false, 0),
		;

		public static final MomentOperate[] NOTICE_OPERATE = { DIG, DONATE, COMMENT_DIG };

		// 定义私有变量
		public final Integer limitCondition;
		public final boolean addOrReduce;
		public Class<?> clazz;
		public final Integer value;
		final ValueProxy<MomentOperate, Integer> proxy;

		// 构造函数，枚举类型只能为私有
		MomentOperate(Integer value, String label, boolean addOrReduce, Integer limitCondition, Class<?> clazz) {
			this.value = value;
			this.limitCondition = limitCondition;
			this.clazz = clazz;
			this.addOrReduce = addOrReduce;
			this.proxy = new ValueProxy<>(this, value, label);
		}

		MomentOperate(Integer value, String label, boolean addOrReduce, Integer limitCondition) {
			this(value, label, addOrReduce, limitCondition, null);
		}

		MomentOperate(Integer value, String label, boolean addOrReduce, Class<?> clazz) {
			this(value, label, addOrReduce, null, clazz);
		}

		public static MomentOperate[] getOperate(MomentOperate type) {
			switch (type) {
				case DIG:
				case CANCEL_DIG:
					return new MomentOperate[] { DIG, CANCEL_DIG };
				case COMMENT_DIG:
				case CANCEL_COMMENT_DIG:
					return new MomentOperate[] { COMMENT_DIG, CANCEL_COMMENT_DIG };
				case DONATE:
					return new MomentOperate[] { DONATE };
			}
			return null;
		}

		public static MomentOperate[] getValues() {
			return ValueProxy.getCachedArray(MomentOperate.class, MomentOperate::values);
		}

		public static MomentOperate of(Integer value) {
			return DIG.getValueOf(value);
		}
	}

	public static final String USER_ID = "user_id";
	public static final String TO_USER_ID = "to_user_id";
	public static final String TYPE = "type";
	public static final String MOMENT_ID = "moment_id";
	public static final String COMMENT_ID = "comment_id";
	public static final String REMARK = "remark";
	public static final String COUNT = "count";
	public static final String HANDLE_STATUS = "handle_status";

}
